c_args = ['-DSTM32F10X_HD']
c_link_args = []
link_deps = []
project_inc = [
  include_directories('./'),
  include_directories('../STM32F10x_StdPeriph_Lib_V3.6.0/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/'),
  include_directories('../STM32F10x_StdPeriph_Lib_V3.6.0/Libraries/CMSIS/CM3/CoreSupport/'),
  include_directories('../STM32F10x_StdPeriph_Lib_V3.6.0/Libraries/STM32F10x_StdPeriph_Driver/inc/'),
  ]
project_src = files('main.c', 'system_stm32f10x.c', 'stm32f10x_it.c')
size = find_program('arm-none-eabi-size')
objcopy = find_program('arm-none-eabi-objcopy')

link_args = ['../STM32F10x_StdPeriph_Lib_V3.6.0/Project/STM32F10x_StdPeriph_Template/TrueSTUDIO/STM3210E-EVAL/stm32_flash.ld']
linkerfile = files('../STM32F10x_StdPeriph_Lib_V3.6.0/Project/STM32F10x_StdPeriph_Template/TrueSTUDIO/STM3210E-EVAL/stm32_flash.ld')
startupfile = files('../STM32F10x_StdPeriph_Lib_V3.6.0/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup/gcc_ride7/startup_stm32f10x_hd.s')

c_link_args += '-Wl,-T@0@/../@1@'.format(meson.current_source_dir(), linkerfile[0])
#message(linkerfile[0])
message(meson.current_source_dir())
message(c_link_args)
main = executable('main',
  [project_src, startupfile],
  name_suffix : 'elf',
  c_args : c_args,
  link_args : [c_link_args, '-Wl,--gc-sections'],
  dependencies : [link_deps, libmcu_dep],
  include_directories : [project_inc])

# cutom target to always convert main.elf to binary
mainbin = custom_target('main.bin',
  output : 'main.bin',
  build_by_default : true,
  command : [objcopy, ['-O', 'binary', '-S', main.full_path(), 'main.bin']],
  depends : main)

# ouput size of the .elf
run_target('size',
  command : [size, main.full_path()],
  depends : main)
if false
# upload the binary to the microcontroller
if not stmprog.found()
  warning('STM32_Programmer_CLI not found, flashing not possible')
else
  run_target('flash',
    command : [stmprog, '-c', 'port=SWD', '-w', mainbin.full_path(), '0x08000000', '-v', '-hardRst'],
    depends : mainbin)
endif
endif
